package cn.jingzhuan.element.demo.view.util

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.BitmapDrawable
import android.view.View
import jp.wasabeef.glide.transformations.internal.FastBlur


object BlurImageUtil {

    fun blur(bkg: Bitmap, view: View) {
        var scaleFactor = 8f
        var radius = 2f
        var overlay = Bitmap.createBitmap((view.measuredWidth / scaleFactor) as Int,
                (view.measuredHeight / scaleFactor) as Int, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(overlay)
        canvas.translate(-view.left / scaleFactor, -view.top / scaleFactor)
        canvas.scale(1 / scaleFactor, 1 / scaleFactor)
        val paint = Paint()
        paint.flags = Paint.FILTER_BITMAP_FLAG
        canvas.drawBitmap(bkg, 0f,0f,paint)

        // 模糊处理
        overlay = FastBlur.blur(overlay, radius.toInt(), true)
        // 增加透明度未40%的黑色
        canvas.save()
        canvas.drawARGB(102, 0, 0, 0)
        canvas.restore()

        view.background = BitmapDrawable(view.resources, overlay)
    }
}